package com.eric.downloader;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.alibaba.fastjson.JSONObject;

public class Parser {
	
	/**
	 * Extract Baidu Recomendation on Keywords
	 * @param serpHtml the downloaded SERP page in pure text format 
	 * @param wd the query word
	 * @return Json format : {kw:recList}
	 */
	public JSONObject baiduRec (String wd,String serpHtml) {
		Document doc = Jsoup.parse(serpHtml);
		
		JSONObject json = new JSONObject();
		List<String> list = new ArrayList<>();
		String[] a = doc.getElementsByAttributeValue("id", "rs").select("th").text().split(" ");
		for (int i=0;i<a.length;i++) {
			list.add(a[i]);
		}
		json.put(wd, list);
		return json;
	}
	
	/**
	 * Extract from Baidu Pull Down Recommendation Menu
	 * @param serpHtml the downloaded SERP page in pure text format 
	 * @param wd the query word
	 * @return Json format : {kw:recList}
	 */
	public JSONObject baiduPull (String wd,String serpHtml) {
		JSONObject json = new JSONObject();
		
		int p1=0,p2 = 0;
		
		//Parse s String
		List<String> list = new ArrayList<String>();
		p1=0;
		p2=0;
		String s;
		s = serpHtml.substring(serpHtml.indexOf("[")+1,serpHtml.indexOf("]"));
		for (int i=0;i<s.length();i++) {
			if (String.valueOf(s.charAt(i)).equals("\"")) {
				if(p1 < p2 || p1 == 0) {
					p1 = i + 1;
				}else{
					p2 = i;
					list.add(s.substring(p1,p2));
				}
			}
		}
		json.put(wd, list);
		return json;
	}
}
